<?php

class OrderController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        
    }

    public function canceledAction()
    {
    	// action body
    }

    public function deliveredAction()
    {
    
    }
    public function enq1Action()
    {
    	// action body
    	$form = new Application_Form_Oenquiry();
    	$form->submit->setLabel('search');
    	$this->view->form = $form;
    	
    	if ($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData))
    		{
    			$order_no = (double)$form->getValue('order_no');
    			$cust_no = $form->getValue('cust_no');
    			
    			 
    			
    			$params = null;
    		
    			if ($order_no != null)
    			{
    				if ($params == null) {
    					$params = array('order_no' => $order_no);
    				} else {
    					$params = array_merge($params, array('order_no' => $order_no));
    				}
    			}
    			 
    			if($cust_no != Null)
    			{
    				if($params == null) {
    					$params = array('cust_no' => $cust_no);
    				} else {
    					$params = array_merge($params, array('cust_no' => $cust_no));
    				}
    			
    			
    			}
    			if ($params != null)
    			{
    				$this->_helper->redirector('enquiry', 'order', null, $params);
    			}
    			
    			if($order_no == null && $cust_no == null )
    			{
    				$this->view->checker = true;
    			}
    			else
    			{
    				$this->view->checker=false;
    			}
    			
    			
    		}
    		else
    		{
    			$form->populate($formData);
    		}
    		
    	}
    	else 
    	{
    		$request = $this->getRequest();
    		
    		$order_no = (double)$request->getParam('order_no');
    		$cust_no = $request->getParam('cust_no');
    		
    				
    		    		
    		if ($order_no == null) {
    			$order_no = '';
    		}
    		if($cust_no == null) {
    			$cust_no = '';
    		}
    		
    		
    		
    		
    		$orders = new Application_Model_DbTable_Orders();
    		$select = $orders->select()
    		->where('order_no = ?', $order_no)
    		->orWhere('cust_no = ?', $cust_no);
    		
    		$this->view->orders = $orders->fetchAll($select);
    		
    	}
    	
    }
    public function enquiryAction()
    {
    	$form = new Application_Form_Oenquiry();
    	$form->submit->setLabel('search');
    	$this->view->form = $form;
    	
    	
    	if ($this->getRequest()->isPost()) {
    		$formData = $this->getRequest()->getPost();
    		
    		if ($form->isValid($formData)) {
    
    			$order_no = (double)$form->getValue('order_no');
    			$cust_no = $form->getValue('cust_no');
    			
    			$params = null;
    			
    			if ($order_no != null)
    			{
    				if ($params == null) {
    					$params = array('order_no' => $order_no);
    				} else {
    					$params = array_merge($params, array('order_no' => $order_no));
    				}
    			}
    
    			if($cust_no != Null)
    			{
    				if($params == null) {
    					$params = array('cust_no' => $cust_no);
    				} else {
    					$params = array_merge($params, array('cust_no' => $cust_no));
    				}
    					
    					
    			}
    			if ($params != null) 
    			{
    				$this->_helper->redirector('enquiry', 'order', null, $params);
    			} 
    			
    		if($order_no == null && $cust_no == null )
    			{
    				$this->view->checker = true;
    			}
    			else
    			{
    				$this->view->checker=false;
    			}
    		} 
    		else 
    		{
    			$form->populate($formData);
    			
    		}
    	}//end of if post
    	 else
    	 {
    		$request = $this->getRequest();
    		
    		$order_no = (double)$request->getParam('order_no');
    		$cust_no = $request->getParam('cust_no');
    		$flag = false;
    		
    		
    		if ($order_no == null) {
    			$order_no = '';
    		}
    		if($cust_no == null) {
    			$cust_no = '';
    		}
    		
    		$orders = new Application_Model_DbTable_Orders();
    		$select = $orders->select()
    		->where('order_no = ?', $order_no)
    		->orWhere('cust_no = ?', $cust_no);
    		$result = $orders->fetchAll($select);
    		foreach($result as $order)
    		{
    			$index = $order->order_no;
    			$value = $order->order_conf;
    			if($value == "N")
    			{
    				$orders->deleteOrder($index);
    				$flag = true;
    				
    			}
    		}
    		
    		if($flag)
    		{
    			$this->_helper->redirector('cart', 'order', null, array('conf'=>"delConf", 'order_no'=>$order_no, 'cust'=>$cust_no));
    			
    		}
    		
    		$orders = new Application_Model_DbTable_Orders();
    		$select = $orders->select()
    		->where('order_no = ?', $order_no)
    		->orWhere('cust_no = ?', $cust_no);
    		$this->view->orders = $orders->fetchAll($select);
    
    	}
    	
    }

    public function cancelAction()
    {
    	 
    	 
    if($this->getRequest()->isPost())
    	{
    		$del = $this->getRequest()->getPost('del');
    		if($del=='Yes')
    		{
    			$order_no = $this->getRequest()->getPost('order_no');
    			$order_status = $this->getRequest()->getPost('order_status');
    			 
    			$orders = new Application_Model_DbTable_Orders();
    			$items = new Application_Model_DbTable_Items(); 
    			 
    			if($order_status =="D")
    			{
    				$this->_helper->redirector('delivered');
    			}
    			elseif($order_status == "C")
    			{
    				$this->_helper->redirector('canceled');
    			}
    			else{
    				$orders->cancelOrder($order_no);
    				$items->cancelorderItem($order_no);
    				
    			}
    		}
    		$this->_helper->redirector('enquiry');
    	}
    	else
    	{
    		$order_no = $this->_getParam('order_no', 0);
    		$orders = new Application_Model_DbTable_Orders();
    		$this->view->order = $orders->getOrder($order_no);
    	}
    	
    }

    public function editAction()
    {
    	$request = $this->getRequest();
    	 
    	$order_no = (double)$request->getParam('order_no');
    	$cust_no = (int)$request->getParam('cust_no');
    	$staff_no = (int)$request->getParam('staff_no');
    	 
    	$orders = new Application_Model_DbTable_Orders();
    	
    	$customers = new Application_Model_DbTable_Customers();
    	$staffs = new Application_Model_DbTable_Staffs();
    	
    	
    	
    	
    	$selectOrders = $orders->select()
    	->where('order_no = ?', $order_no);
    	$this->view->orders = $orders->fetchRow($selectOrders);
    	 
    	
    	
    	$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    		'host'     => 'amun1.eng.uts.edu.au',
    	    'port'	   => '5006',
    		'username' => 'test',
    		'password' => '1234',
    		'dbname'   => 'tops'
    	    	));
    	$db ->setFetchMode(Zend_Db::FETCH_OBJ);
    	$selectItems = $db->select()
    	    	->from(array('i'=>'items'),
    	array('prod_no', 'item_status', 'item_qty', 'item_no', 'order_no', 'item_total_price')
    	)
    	->join(array('p'=>'products'),
    	    	    	    	('i.prod_no = p.prod_no')
    	    	)
    	    	->where('order_no = ?', $order_no);
    	
    	$this->view->dbs = $db->fetchAll($selectItems);
    	    	    	    	 
    	
    	$selectCustomers = $customers->select()
    	->limit(1)
    	->where('cust_no = ?', $cust_no);
    	$this->view->customers = $customers->fetchRow($selectCustomers);
    	
    	$selectStaffs = $staffs->select()
    	->limit(1)
    	->where('staff_no = ?', $staff_no);
    	$this->view->staffs = $staffs->fetchRow($selectStaffs);
    	 
    	
    	 
    }

    

    public function itemdelAction()
    {
        
    	
    	$item_no = $this->_getParam('item_no', 0);
    	$items = new Application_Model_DbTable_Items();
    	$this->view->items = $items->getItem($item_no);
    	
    	$orders = new Application_Model_DbTable_Orders();
    	
    	$selectItems = $items->select()
    				->where('item_no=?', $item_no);
    	$item = $items->fetchRow($selectItems);
    	$item_price = $item->item_total_price;
    	$order_no = $item->order_no;

    	$selectOrders = $orders->select()
    				->where('order_no = ?', $order_no);
    	$order = $orders->fetchRow($selectOrders);
    	$order_price = $order->order_price;
    	
    	
    	$result_price = $order_price - $item_price;
    	    	
    	
    	$orders->itemdelOrder($order_no, $result_price);
    	$items->deleteItem($item_no);
    	$this->_helper->redirector('cart', 'order');
    }

    public function cartAction()
    {
    	// action body
    	$order_ses = new Zend_Session_Namespace('Order');
    	
    	$base = $this->view->baseUrl();
    	
    	$form = new Application_Form_Order();
    	$form->submit->setLabel('Add');
    	$form->cust_no->setDescription('<a class="search" href="'.$base.'/customer/enquiry">Customer Search</a>')
    		->getDecorator('Description')->setOption('escape', false);
    	$form->prod_no->setDescription('<a class="search" href="'.$base.'/product/enquiry">Product Search</a>')
    		->getDecorator('Description')->setOption('escape', false);
    	$this->view->form = $form;
    	 
    	if ($this->getRequest()->isPost())
    	{
    		$formData = $this->getRequest()->getPost();
    		if ($form->isValid($formData))
    		{
    			 
    			$cust_no = (int)$form->getValue('cust_no');
    			$prod_no = (int)$form->getValue('prod_no');
    			$item_qty = (int)$form->getValue('item_qty');
    			 
    			$date = new Zend_Date();
    			$order_date = $date->get('yyyy-MM-dd');
    			 
    			if ($order_ses->order_no)
    			{
    				$order_ses_check = true;
    				$order_no = $order_ses->order_no;
    			}
    			else
    			{
    				$order_ses_check = false;
    				$order_no = $date->get('yyMMddHHmmssS');
    				$order_ses->order_no = $order_no;
    				
    			}
    			 
    			$products = new Application_Model_DbTable_Products();
    			$select = $products->select()
    			->where('prod_no = ?', $prod_no)
    			->limit(1);
    			$product = $products->fetchRow($select);
    			$order_price = $product->prod_price;
    			 
    			$item_total_price = $item_qty * $order_price;
    			 
    			$auth = Zend_Auth::getInstance();
    			$staff_no = $auth->getIdentity()->staff_no;
    			 
    			if ($order_ses_check == false)
    			{
    				$orders = new Application_Model_DbTable_Orders();
    				$orders->addOrder($order_no, $order_date, 'B', $item_total_price, $staff_no, $cust_no);
    				
    			}
    			 
    			$items = new Application_Model_DbTable_Items();
    			$items->addItem($order_no, $prod_no, 'A', $item_qty, $item_total_price);
    			 
    			$orders = new Application_Model_DbTable_Orders();
    			$selectOrders = $orders->select()
    			->where('order_no =?', $order_no);
    			$order = $orders->fetchRow($selectOrders);
    			$current_price =$order->order_price;
    			 
    			$order_total_price = $current_price+$item_total_price;
    			 
    			if($order_ses_check == true)
    			{
    				$orders->updateOrder($order_no, $order_total_price);
    			}
    			$this->_helper->redirector('cart', 'order');
    		}//end of form valid
    		else
    		{
    			$form->populate($formData);
    		}
    	}//end of post checking
    	else
    	{
    		$request = $this->getRequest();
    		
    		$cond = $request->getParam('conf');
    		$order_confirm = $request->getParam('order_no');
    		$cust = $request->getPAram('cust');
    		if($cond == "yes")
    		{
    			Zend_Session:: namespaceUnset('Order');
    			$this->_helper->redirector('confirm', 'order', null, array('order_no'=>$order_confirm));
    		}
    		elseif($cond == "del")
    		{
    			Zend_Session:: namespaceUnset('Order');
    			
    			$this->_helper->redirector('cart', 'order');
    		}
    		elseif($cond == "delConf")
    		{
    			Zend_Session:: namespaceUnset('Order');
    			$this->_helper->redirector('enquiry', 'order', null, array('order_no'=>$order_confirm, 'cust_no'=>$cust));
    		}
    		
    		$cust_no = $this->_getParam('cust_no', 0);
    		$prod_no = $this->_getParam('prod_no', 0);
    		if ($cust_no > 0) {
    			$order_ses->cust_no = $cust_no; // Session(Order)
    		}
    		if ($prod_no > 0) {
    			$order_ses->prod_no = $prod_no; // Session(Order)
    		}
    		if ($order_ses->cust_no != null) {
    			$form->cust_no->setValue($order_ses->cust_no);
    		}
    		if ($order_ses->prod_no != null) {
    			$form->prod_no->setValue($order_ses->prod_no);
    		}
    		$order_ses->setExpirationSeconds(600);
    		 
    		if (Zend_Session::namespaceIsset('Order')) {
    			if ($order_ses->order_no) {
    
    					
    				$db = Zend_Db::factory('Pdo_Mysql', array(
        	        			    	     	'host'     => 'amun1.eng.uts.edu.au',
        	        			    	        'port'	   => '5006',
        	        			    	        'username' => 'test',
        	        			    	        'password' => '1234',
        	        			    	        'dbname'   => 'tops'
    				));
    				$db->setFetchMode(Zend_Db::FETCH_OBJ);
    					
    				$select = $db->select()
    				->from(array('i' => 'items'), array('order_no', 'prod_no', 'item_qty', 'item_no'))
    				->join(array('p' => 'products'), ('i.prod_no = p.prod_no'))
    				->where('order_no = ?', $order_ses->order_no);
    					
    				$this->view->dbs = $db->fetchAll($select);
    
    				$orders = new Application_Model_DbTable_Orders();
    				$select = $orders->select()
    				->where('order_no = ?', $order_ses->order_no);
    				$this->view->orders = $orders->fetchAll($select);
    			}
    		}
    		 
    
    		//$this->view->cust_no = $order_ses->cust_no;
    		//$this->view->prod_no = $order_ses->prod_no;
    	}
    	// End - ReadParam //
    
       
    }
    public function reviewAction()
    {
    
    	$request = $this->getRequest();
    	 
    	$order_no = (double)$request->getParam('order_no');
    	 
    	$orders = new Application_Model_DbTable_Orders();
    	$staffs = new Application_Model_DbTable_Staffs();
    	$customers = new Application_Model_DbTable_Customers();
    
    
    	$selectOrders = $orders->select()
    	->where('order_no = ?', $order_no);
    	$this->view->orders = $orders->fetchRow($selectOrders);
    	$order = $orders->fetchRow($selectOrders);
    	$staff_no = $order->staff_no;
    	$cust_no = $order->cust_no;
    	 
    
    
    	$db = new Zend_Db_Adapter_Pdo_Mysql(array(
        	    	'host'     => 'amun1.eng.uts.edu.au',
        	    	 'port'	   => '5006',
        	    	'username' => 'test',
        	    	'password' => '1234',
        	    	'dbname'   => 'tops'
    	));
    	 
    	$db ->setFetchMode(Zend_Db::FETCH_OBJ);
    	 
    	$selectItems = $db->select()
    	->from(array('i'=>'items'),
    	array('prod_no', 'item_status', 'item_qty', 'item_no', 'order_no', 'item_total_price')
    	)
    	->join(array('p'=>'products'),
    	('i.prod_no = p.prod_no')
    	)
    	->where('order_no = ?', $order_no);
    	$this->view->dbs = $db->fetchAll($selectItems);
    	 
    	 
    	$selectStaffs = $staffs->select()
    	->limit(1)
    	->where('staff_no = ?', $staff_no);
    	$this->view->staffs = $staffs->fetchRow($selectStaffs);
    	 
    	 
    	$selectCustomers = $customers->select()
    	->limit(1)
    	->where('cust_no = ?', $cust_no);
    	$this->view->customers = $customers->fetchRow($selectCustomers);
    
    }

    public function confirmAction()
    {
        // action body
    
    	$request = $this->getRequest();
    	 
    	$order_no = (double)$request->getParam('order_no');
    	 
    	$orders = new Application_Model_DbTable_Orders();
    	$staffs = new Application_Model_DbTable_Staffs();
    	$customers = new Application_Model_DbTable_Customers();
    	
    	
    	$selectOrders = $orders->select()
    	->where('order_no = ?', $order_no);
    	$this->view->orders = $orders->fetchRow($selectOrders);
    	$order = $orders->fetchRow($selectOrders);
    	$staff_no = $order->staff_no;
    	$cust_no = $order->cust_no;
    	 
    	$orders->confOrder($order_no);
    	
    	
    	$db = new Zend_Db_Adapter_Pdo_Mysql(array(
    	    	    	'host'     => 'amun1.eng.uts.edu.au',
    	    	    	 'port'	   => '5006',
    	    	    	'username' => 'test',
    	    	    	'password' => '1234',
    	    	    	'dbname'   => 'tops'
    	));
    	 
    	$db ->setFetchMode(Zend_Db::FETCH_OBJ);
    	 
    	$selectItems = $db->select()
    	->from(array('i'=>'items'),
    	array('prod_no', 'item_status', 'item_qty', 'item_no', 'order_no', 'item_total_price')
    	)
    	->join(array('p'=>'products'),
    	('i.prod_no = p.prod_no')
    	)
    	->where('order_no = ?', $order_no);
    	$this->view->dbs = $db->fetchAll($selectItems);
    	 
    	 
    	$selectStaffs = $staffs->select()
    	->limit(1)
    	->where('staff_no = ?', $staff_no);
    	$this->view->staffs = $staffs->fetchRow($selectStaffs);
    	 
    	 
    	$selectCustomers = $customers->select()
    	->limit(1)
    	->where('cust_no = ?', $cust_no);
    	$this->view->customers = $customers->fetchRow($selectCustomers);
    	
    	
    	
    }

    public function deleteAction()
    {
        // getorder no-> delete items which has order no->delete order -> redirector-> cart (for end session)
    	$request = $this->getRequest();
    	$order_no = (double)$request->getParam('order_no');
    	$order_del = "del";
    	
    	$orders = new Application_Model_DbTable_Orders();
    	$items = new Application_Model_DbTable_Items();
    	$selectItems = $items->select()
    			->where('order_no =?', $order_no);
    	
    	$result = $items->fetchAll($selectItems);
    	
    	/*$stmt = $selectItems->query();
        $result = $stmt->fetchAll();
    	*/
    	
        foreach($result as $index):
			$no = $index->item_no;
			$items->deleteItem($no);
        endforeach;
        $orders->deleteOrder($order_no);
        $this->_helper->redirector('cart', 'order', null, array('conf'=>$order_del));
        
    
    }

    


}












